using TMPro;
using UnityEngine;
using UnityEngine.UI;
//控制商品槽UI
public class ShopSlotUI : MonoBehaviour
{
    [Header("商品图标组件")]
    [SerializeField] private Image _itemSprite;
    [Header("商品名称的组件")]
    [SerializeField] private TextMeshProUGUI _itemName; 
    [Header("商品数量的组件")]
    [SerializeField] private TextMeshProUGUI _itemCount;
    [Header("该UI对应的商店物品槽")]
    [SerializeField] private ShopSlot _assignedItemSlot; 
    public ShopSlot AssignedItemSlot => _assignedItemSlot;
    [Header("将商品添加到购物车的按钮")]
    [SerializeField] private Button _addItemToCartButton;
    [Header("从购物车中移除商品的按钮")]
    [SerializeField] private Button _removeItemFromCartButton;

    private int _tempAmount;// 记录临时数量，初始化为物品槽的堆叠数量

    // 声明关联的商店窗口显示对象和价格调整比例
    public ShopKeeperDisplay ParentDisplay { get; private set; }
    public float MarkUp { get; private set; }//加价比例

    private void Awake()
    {
        // 初始化UI元素
        _itemSprite.sprite = null;
        _itemSprite.preserveAspect = true;
        _itemSprite.color = Color.clear;
        _itemName.text = "";
        _itemCount.text = "";

        // 添加按钮点击监听事件
        _addItemToCartButton?.onClick.AddListener(AddItemToCart);
        _removeItemFromCartButton?.onClick.AddListener(RemoveItemFromCart);

        // 获取关联的商店窗口显示对象
        ParentDisplay = transform.parent.GetComponentInParent<ShopKeeperDisplay>();
    }

    // 初始化商店物品槽UI
    public void Init(ShopSlot slot, float markup)
    {
        _assignedItemSlot = slot;
        MarkUp = markup;
        _tempAmount = slot.StackSize;// 将临时数量初始化为物品槽的堆叠数量
        UpdateUISlot();
    }

    // 更新商店物品槽UI的显示
    private void UpdateUISlot()
    {
        if (_assignedItemSlot.ItemData != null)
        {
            // 显示物品图标、名称、数量和价格
            _itemSprite.sprite = _assignedItemSlot.ItemData.Icon;
            _itemSprite.color = Color.white;
            _itemCount.text = _assignedItemSlot.StackSize.ToString();

            // 获取调整后的物品价格 
            var modifiedPrice = ShopKeeperDisplay.GetModifiedPrice(_assignedItemSlot.ItemData, 1, MarkUp);
            // 显示物品名称和价格
            _itemName.text = $"{_assignedItemSlot.ItemData.DisplayName} - {modifiedPrice}G";
        }
        else
        {
            // 如果物品为空，则清空显示
            _itemSprite.sprite = null;
            _itemSprite.color = Color.clear;
            _itemName.text = "";
            _itemCount.text = "";
        }
    }

    // 从购物车中移除物品
    private void RemoveItemFromCart()
    {
        Debug.Log("从购物车中移除物品");
        // 如果临时数量等于物品槽堆叠数量，则不做任何操作，直接返回
        if (_tempAmount == _assignedItemSlot.StackSize) return;
        _tempAmount++;
        ParentDisplay.RemoveItemFromCart(this);// 从购物车中移除该物品槽对应的物品
        _itemCount.text = _tempAmount.ToString();// 更新物品数量的文本显示
    }

    // 将物品添加到购物车中
    private void AddItemToCart()
    {
        Debug.Log("向购物车中添加商品");
        // 如果临时数量小于等于 0，则不做任何操作，直接返回
        if (_tempAmount <= 0) return;
        _tempAmount--;
        ParentDisplay.AddItemToCart(this);// 将该物品槽对应的物品添加到购物车中
        _itemCount.text = _tempAmount.ToString();// 更新物品数量的文本显示
    }
}
